using System;
using System.Data;
using MyClever.Lib;

namespace MyClever.DefaultPlugins
{
    public class MergePackages : MissionPlugin
    {
        [Package(Direction.Input, "Defines the origin package in which we like merge another package.")]
        enum OriginPackage { }

        [Package(Direction.Input, "Defines the package which should merge in the originPackage.")]
        enum PackageToMerge { }

        [Package(Direction.Input, "A ruleset which defines the merge-process.")]
        enum MergeNorm 
        {
            [Column(typeof(string), "The column which contains an unique identifier as merge purchase.")]
            OriginPackageUniqueID,
            [Column(typeof(string), "The column which contains the same unique identifier like the OriginPackageUniqueID.")]
            PackageToMergeUniqueID
        }

        [Package(Direction.Output, "The new package with all merged data.")]
        enum Output
        { }

        [Plugin(PluginDevStatus.Production, "Merges two packages by using an unique identifier.")]
        public MergePackages()
        {
        }

        public override void Initialize()
        {
            //This is the first Function called by the host...
            //Put anything needed to start with here first
        }

        public override void Dispose()
        {
            //Put any cleanup code in here for when the program is stopped
        }

        protected override void Work()
        {
            MissionPluginPackage outputPackage = this.GetPackageByName(typeof(Output));
            outputPackage.CopyDataFromPackage(this.GetPackageByName(typeof(OriginPackage)));

            DataTableReader mergeNormPackage = this.GetDataReader(typeof(MergeNorm));
            mergeNormPackage.Read();            
            string originPackageUniqueColumnName = mergeNormPackage[MergeNorm.OriginPackageUniqueID.ToString()].ToString();
            string packageToMergeUniqueColumnName = mergeNormPackage[MergeNorm.PackageToMergeUniqueID.ToString()].ToString();
            mergeNormPackage.Close();

            this.AddNewLoggingMessage(typeof(MergeNorm), String.Format("merge origin column: '{0}' with merge-package column: '{1}'", originPackageUniqueColumnName, packageToMergeUniqueColumnName));

            foreach (MissionPluginPackageColumn columnOn in this.GetPackageByName(typeof(PackageToMerge)).Columns)
            {
                if (packageToMergeUniqueColumnName != columnOn.ColumnName)
                {
                    outputPackage.Columns.Add(new MissionPluginPackageColumn(columnOn.ColumnName, columnOn.DataType));    
                }
            }

            foreach (DataRow rowOn in outputPackage.Rows)
            {
                string originUniqueID = rowOn[originPackageUniqueColumnName].ToString();
                string querySelectMergeRows = String.Format("[{0}] = '{1}'", packageToMergeUniqueColumnName, originUniqueID);

                DataRow[] foundRows = this.GetPackageByName(typeof(PackageToMerge)).Select(querySelectMergeRows);
                if (foundRows.Length > 0)
                {
                    foreach (MissionPluginPackageColumn columnOn in this.GetPackageByName(typeof(PackageToMerge)).Columns)
                    {
                        if (packageToMergeUniqueColumnName != columnOn.ColumnName)
                        {
                            rowOn[columnOn.ColumnName] = foundRows[0][columnOn.ColumnName];
                        }
                    }   
                }
            }
        }  
    }
}
